﻿using System;
using System.Linq;
using ASPNETVN.PORTAL.Components.UI;
using ASPNETVN.PORTAL.Components.Helpers;
using System.Net;
using System.IO;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using CDTM.Modules.ONLINE.Components;
using ASPNETVN.PORTAL.Components.Share;

namespace CDTM.Modules.ONLINE.Controls
{
    public enum NickType
    {
        Yahoo,
        Skype
    }

    public partial class Style1 : ModulePage
    {
        private static List<Online> _list = new List<Online>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                GridDataBinding();
            }
        }

        private string FixName(string nickName)
        {
            string sRet = nickName;

            if (!String.IsNullOrEmpty(nickName))
            {
                //Xóa các ký tự '
                //sRet = nickName.Replace("'", "");

                //Tìm kí tự $ trong tên Sheet
                int iLastIndex = sRet.LastIndexOf("@");
                if (iLastIndex != -1)//Tìm thấy, loại bỏ đoạn từ $ về cuối string
                    sRet = sRet.Substring(0, iLastIndex);
            }
            return sRet;
        }

        private bool ValidateIsOnline(string nickname, NickType type)
        {
            HttpWebRequest objRequest = null;
            switch (type)
            {
                case NickType.Yahoo:
                    objRequest = (HttpWebRequest)WebRequest.Create("http://opi.yahoo.com/online?u=" + FixName(nickname) + "&m=s&t=8");
                    break;
            }

            HttpWebResponse objResponse = null;

            try
            {
                objResponse = (HttpWebResponse)objRequest.GetResponse();
            }
            catch (Exception ex) { }

            if (objResponse != null)
            {
                StreamReader objReader = new StreamReader(objResponse.GetResponseStream());
                string msgReturn = objReader.ReadToEnd();
                objReader.Close();
                objResponse.Close();
                msgReturn = msgReturn.ToLower().Trim();


                if (msgReturn.EndsWith("is online"))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

            return false;
        }

        private void GridDataBinding()
        {
            _list = Components.Online.SelectCollectionAll(this.ModuleID).Where(x => x.TrangThai == true && x.CategoryID == 0).ToList();
            rep.DataSource = _list;
            rep.DataBind();
        }

        private DataTable BindData()
        {
            DataTable dt = new DataTable();

            dt.Columns.AddRange(new DataColumn[]{ 
                new DataColumn("NoiHoTro", typeof(String)),
                new DataColumn("Nickname", typeof(String)),
                new DataColumn("NickType", typeof(String))});

            DataRow row = dt.NewRow();
            row["NoiHoTro"] = "Phòng đào tạo";
            row["Nickname"] = "sontelecom79";
            row["NickType"] = NickType.Yahoo.ToString();
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["NoiHoTro"] = "Phòng giáo viên";
            row["Nickname"] = "taquangvan";
            row["NickType"] = NickType.Yahoo.ToString();
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["NoiHoTro"] = "Phòng sinh viên";
            row["Nickname"] = "buivan1511";
            row["NickType"] = NickType.Yahoo.ToString();
            dt.Rows.Add(row);

            return dt;
        }

        protected void rep_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            HiddenField hdfID = (HiddenField)e.Item.FindControl("hdfID");

            Literal ltOnline = (Literal)e.Item.FindControl("ltOnline");
            Literal ltOffline = (Literal)e.Item.FindControl("ltOffline");
            HyperLink linkOnline = (HyperLink)e.Item.FindControl("linkOnline");
            HyperLink linkOffline = (HyperLink)e.Item.FindControl("linkOffline");

            Online item = _list.Find(delegate(Online obj) { return obj.ID == int.Parse(hdfID.Value); });

            if (item == null) return;

            NickType type = (NickType)Enum.Parse(typeof(NickType), item.NickType, true);

            if (ValidateIsOnline(item.NickName, type) == true)
            {
                ltOnline.Text = "<div class='OnlineImageOn'></div>";
                linkOnline.NavigateUrl = "ymsgr:sendim?" + item.NickName;
                linkOnline.ToolTip = item.HoTen;

                ltOffline.Text = "<div class='OfflineImageOff'></div>";
            }
            else
            {
                ltOnline.Text = "<div class='OnlineImageOff'></div>";
                linkOnline.NavigateUrl = "";

                ltOffline.Text = "<div class='OfflineImageOn'></div>";
                linkOffline.ToolTip = item.HoTen;
            }
        }

    }
}